package Concurrent;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description：
 * Author: zhangc
 * Date：2017/8/16 15:46
 */
public class ThreadFactory implements java.util.concurrent.ThreadFactory {

    private static AtomicInteger atomicInteger = new AtomicInteger(0);

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setName(String.valueOf(atomicInteger.getAndIncrement()));
        return thread;
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactory());
        Future<Integer>[] futures = new Future[ 100 ];
        AtomicInteger atomicInteger = new AtomicInteger(0);
        for (int i = 0; i < 100; i++) {
            futures[ i ] = executorService.submit(() -> {
                Thread.sleep(1000);
                return atomicInteger.getAndIncrement();
            });
        }
        for (int i = 0; i < 100; i++) {
            try {
                System.out.println(futures[ i ].get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        //executorService.shutdown();
    }
}
